import App from './App'
import i18n from './lang/i18n'
import request from './utils/request'
import { baseUrl } from './utils/request'
import apiUrls from './utils/apiUrl'
import { setCurrentUrl,redirectTo } from './utils/loginUtil'
import dict from './utils/dict'
import * as Pinia from 'pinia';
import { createSSRApp } from 'vue'
import pwdLoginVue from './pages/ucenter/login/pwdLogin.vue'

export function createApp() {
	const app = createSSRApp(App)
	app.use(i18n)
	app.use(Pinia.createPinia())
	// 网络访问方法
	app.config.globalProperties.$baseUrl = baseUrl
	// 静态文件路径
	app.config.globalProperties.$static = 'https://www.ccbckj.com/gkms/profile'
	
	app.config.globalProperties.$api = apiUrls
	app.config.globalProperties.$dict = dict.loadData
	app.config.globalProperties.$get = (url, data) => {
		return request(url, data, 'GET')
	}
	app.config.globalProperties.$post = (url, data, header) => {
		return request(url, data, 'POST', header)
	}
	/** 判断是否登录方法 */
	app.config.globalProperties.$isLogin = (goLogin) => {
		// getTabBar()
		const userInfo = uni.getStorageSync('USER_INFO');
		const token = uni.getStorageSync('TOKEN');
		if (token && userInfo && userInfo.name) {
			return true
		}
		// 是否需要跳转到登录页
		if (goLogin) {
			// 当前页面路径保存到Storage
			setCurrentUrl()
			// 页面跳转
			uni.navigateTo({
				url: "/pages/ucenter/login/wxLogin"
			})
		}
		return false
	}
	/** 判断是否登录方法（带确认框） 
	goLogin是否需要跳转至登录页，如果传true没登录则跳转到登录页，不传或者传false则不进行跳转
	cancleBackUrl 模态框取消跳转路径
	*/
	app.config.globalProperties.$isLoginModel = (goLogin, cancleBackUrl) => {
		const userInfo = uni.getStorageSync('USER_INFO');
		const token = uni.getStorageSync('TOKEN');
		if (token && userInfo && userInfo.name) {
			return true
		}
		// 是否需要跳转到登录页
		if (goLogin) {
			uni.showModal({
				title: '提示',
				content: '您处于未登录状态,是否登录？',
				success: function(res) {
					if (res.confirm) {
						// 当前页面路径保存到Storage
						setCurrentUrl()
						// 页面跳转
						uni.navigateTo({
							url: "/pages/ucenter/login/wxLogin"
						})
					} else if (res.cancel) {
						if (cancleBackUrl) {
							redirectTo(cancleBackUrl)
						}
					}
				}
			});
		}
		return false
	}
	/** 获取服务器用户信息 */
	app.config.globalProperties.$syncUserInfo = () => {

		return new Promise((resolve, reject) => {
			request(apiUrls.personal.info).then(res => {
				uni.setStorageSync('USER_INFO',res.user)
				resolve()
			}).catch((ex) => {
				reject(ex)
			})
		})
	}
	/** 登陆成功后跳转 */
	app.config.globalProperties.$loginSuccessGoFun = () => {
		let loginBackUrl = uni.getStorageSync('LOGIN-BACK-URL');
		if (loginBackUrl === "") {
			loginBackUrl = {
				type: "tabbar",
				fullPath: "/pages/index/index"
			}
		}
		redirectTo(loginBackUrl.fullPath)
		uni.removeStorage({
			key: 'LOGIN-BACK-URL',
			success: function(res) {
				console.log('缓存移除LOGIN-BACK-URL成功');
			}
		});
	}
	/** 确认框 */
	app.config.globalProperties.$confirm = (title, content) => {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title,
				content,
				success: function(res) {
					if (res.confirm) {
						return resolve()
					} else {
						return 	reject()
					}
				}
			})
		}).catch((ex) => {
			return reject(ex)
		})
	}

	return {
		app,
		Pinia
	}
}